from functools import lru_cache
from itertools import accumulate
# https://www.lanqiao.cn/problems/2195/learning/?page=1&first_category_id=1&name=%E8%B4%B9%E7%94%A8%E6%8A%A5%E9%94%80
n, M, k = map(int, input().split())
bag = {}  # 某天费用
mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
accm_days = [0] + list(accumulate(mdays))


def getId(m, d):
    return accm_days[m - 1] + d


for _ in range(n):
    m, d, v = map(int, input().split())
    if getId(m, d) not in bag:
        bag[getId(m, d)] = []
    bag[getId(m, d)].append(v)
day_map = [0] * 366
for key, val in bag.items():
    day_map[key] = max(val)

dp = [[0 for _ in range(2)] for _ in range(366)]
for i in range(1,366):
    dp[i][0] = max(dp[i-1])
    dp[i][1] = max(dp[i-k]) + day_map[i]
print(max(dp[365]))